package com.itheima.web.handlers;


import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
public class CommonExceptionHandler {


    //专门处理授权异常
    @ExceptionHandler(AuthorizationException.class)
    public String handlerAuthorizationException(Exception e, HttpServletRequest request) {

        //1 记录异常
        e.printStackTrace();

        //2 返回页面
        return "redirect:/unauthorized.jsp";

    }

    //所有异常
    @ExceptionHandler(Exception.class)
    public String handlerException(Exception e, HttpServletRequest request) {

        //1 记录异常
        e.printStackTrace();
        request.setAttribute("errorMsg", e.getMessage());

        //2 返回页面
        return "error";

    }
}
